package com.share.service.oa.org.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.share.bean.oa.org.EmpAccount;
import com.share.commons.base.query.BaseQuery;
import com.share.commons.util.MD5;
import com.share.dao.oa.org.EmpAccountDao;
import com.share.service.oa.org.EmployeeLoginLicenceManager;

/**
 * 说明：员工登陆验证以及修改登陆密码
 * <br/>作者：杨浩泉
 * <br/>日期：2010-9-4
 */
@Service
public class EmployeeLoginLicenceManagaerImpl implements
		EmployeeLoginLicenceManager {

	@Resource
	private EmpAccountDao accountDao;

	public void setAccountDao(EmpAccountDao accountDao) {
		this.accountDao = accountDao;
	}

	@Override
	public EmpAccount checkEmpLoginManager(String account, String password)
			throws Exception {

		if (null != account && !"".equals(account) && null != password
				&& !"".equals(password)) {
			String hql = "select o from EmpAccount o";

			StringBuffer whereHQL = new StringBuffer();
			whereHQL.append("o.account=?").append(BaseQuery.AND).append("o.password=?");

			List<Object> param = new ArrayList<Object>();
			param.add(account);
			//param.add(password);
			
			param.add(MD5.MD5Encode(password));

			return this.accountDao.checkEmployeeLogin(hql, whereHQL.toString(),param);
		}
		return null;
	}

	/**
	 * 验证权限
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String validateLicenceManager(HashMap<String, Object> map,
			String valUrl) throws Exception {
		
		if (!map.isEmpty()) {
			List role = (List) map.get("role") ;
			if(null != role && !role.isEmpty()) {
				for (int i = 0; i < role.size(); i++) {
					if (role.get(i).equals(valUrl)) {
						System.out.println(role.get(i));
						return "true";
					} 
				}
			}
			
			List res = (List) map.get("res") ;
			if(null != res && !res.isEmpty()) {
				for (int i = 0; i < res.size(); i++) {
					if (res.get(i).equals(valUrl)) {
						return "true";
					} 
				}
			}
		}	
		
		return "false";
	}

	/**
	 * 检查账号存不存在
	 */
	@Override
	public boolean checkAccountExistManager(String accountParam) throws Exception {
		
		String property = "o.account" ;
		StringBuffer whereHQL = new StringBuffer();
		whereHQL.append("o.account=?");
		Object[] param = new Object[]{accountParam};
		
		List<Object> list = this.accountDao.checkAccountExistDao(property, whereHQL.toString(), param) ;
		
		return (null != list && !list.isEmpty()? true : false);
	}

	@Override
	public boolean updateEmpPwdManager(String pwd,int id) throws Exception {
		String hql = "update EmpAccount o" ;
		StringBuffer setHQL = new StringBuffer() ;
		StringBuffer whereHQL = new StringBuffer() ;
		
		setHQL.append("o.password=?") ;
		whereHQL.append("o.id=?") ;
		
		Integer ret = (Integer) this.accountDao.updateEmpPwdDao(hql, setHQL.toString(), whereHQL.toString(), new Object[]{pwd,id}) ;
		
		return (ret > 0 ? true : false) ;
	}
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean-common.xml");
		EmployeeLoginLicenceManager e = (EmployeeLoginLicenceManager) ctx.getBean("employeeLoginLicenceManagaerImpl") ;
		
		try {
			System.out.println(e.updateEmpPwdManager("fffcc", 2));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
	}
}
